home *** CD-ROM | disk | FTP | other *** search
- package symantec.itools.db.pro;
-
- import java.util.Hashtable;
- import java.util.NoSuchElementException;
- import java.util.Properties;
- import java.util.StringTokenizer;
- import java.util.Vector;
- import symantec.itools.db.net.ClientSession;
- import symantec.itools.db.net.Param;
- import symantec.itools.db.net.RemoteObject;
- import symantec.itools.db.net.SQLConnectionException;
- import symantec.itools.db.net.TextParam;
- import symjava.sql.SQLException;
-
- public class Session {
- private boolean _isClosed;
- ClientSession _session;
- RemoteObject _messgr;
- Vector _metadataObjs;
- Vector _multiViews;
- Logon _logonObj;
- ConnectionInfo _defaultConn;
- private String _serverURL;
- private final int METHOD_close;
- private final int METHOD_getMetaData = 1;
- private final int METHOD_createView = 2;
- private final int METHOD_inquireDSN = 3;
-
- public Session(String serverURL) throws SQLException {
- this.connect(serverURL);
- this._serverURL = serverURL;
- Vector params = new Vector();
- int objectID = this._messgr.invokeConstructor(1, params);
- this._messgr.setObject("CSCLClientSession", objectID);
- this._isClosed = false;
- this._defaultConn = new ConnectionInfo("");
- }
-
- public String getServerURL() throws SQLException {
- return this._serverURL;
- }
-
- public void setLogonObject(Logon logonObj) throws SQLException {
- this._logonObj = logonObj;
- }
-
- public void setDefaultConnection(ConnectionInfo conn) throws SQLException {
- this._defaultConn = conn;
- }
-
- public RelationView createView(ConnectionInfo conn, String sql, Properties requestProps) throws SQLException {
- Hashtable ht = RelationView.convertViewProps(requestProps);
- MultiView mv = this.createMultiView(conn, sql, ht);
- this.addMultiView(mv);
- RelationView rv = new RelationView(mv, (byte)0, conn);
- mv.setRootRelView(rv);
- rv.setInitialPosition(requestProps);
- return rv;
- }
-
- MultiView createMultiView(ConnectionInfo conn, String sql, Hashtable ht) throws SQLException {
- if (conn == null) {
- throw new SQLException("ConnectionInfo object is null");
- } else {
- Integer optConc = (Integer)ht.get("optConc");
- Integer maxRows = (Integer)ht.get("maxRecords");
- Boolean readOnly = (Boolean)ht.get("readOnly");
- Boolean sharable = (Boolean)ht.get("sharable");
- String viewName = new String("RelView");
- ht.get("recPosition");
- int retries = 0;
-
- while(true) {
- String username = conn.getUser();
- String password = conn.getPassword();
- String db = conn.getDBString();
- Vector params = new Vector();
- params.addElement(new TextParam(0, db));
- params.addElement(new TextParam(0, viewName));
- params.addElement(new TextParam(0, sql));
- params.addElement(new Param(0, optConc));
- params.addElement(new Param(0, maxRows));
- params.addElement(new Param(0, readOnly));
- params.addElement(new Param(0, sharable));
- params.addElement(new TextParam(0, username));
- params.addElement(new TextParam(0, password));
- params.addElement(new Param(0, conn.getAutoDisconnect()));
-
- try {
- Vector results = this._messgr.invokeMethod(2, params);
- MultiView mv = new MultiView(this, results, conn);
- return mv;
- } catch (SQLConnectionException e) {
- ++retries;
- if (!this.logonFailed(conn, retries)) {
- throw e;
- }
- }
- }
- }
- }
-
- public RelationView createView(ConnectionInfo conn, String tablename, Vector columns, Properties requestProps) throws SQLException {
- String sql = new String("select ");
- if (columns == null) {
- sql = sql + "* ";
- } else if (columns.size() == 0) {
- sql = sql + "* ";
- } else {
- String temp = (String)columns.elementAt(0);
- sql = sql + temp;
-
- for(int i = 1; i < columns.size(); ++i) {
- temp = (String)columns.elementAt(i);
- sql = sql + ", " + temp;
- }
- }
-
- sql = sql + " from " + tablename;
- return this.createView(conn, sql, requestProps);
- }
-
- public RelationView createView(ConnectionInfo conn, String sql) throws SQLException {
- return this.createView(conn, sql, new Properties());
- }
-
- public RelationView createView(ConnectionInfo conn, String tablename, Vector columns) throws SQLException {
- return this.createView(conn, tablename, columns, new Properties());
- }
-
- public RelationView createView(String sql) throws SQLException {
- return this.createView(this._defaultConn, sql);
- }
-
- public RelationView createView(String tablename, Vector columns) throws SQLException {
- return this.createView(this._defaultConn, tablename, columns);
- }
-
- public Request getRequest() throws SQLException {
- return this.getRequest("");
- }
-
- public Request getRequest(String sql) throws SQLException {
- Request request = new Request(this, this._defaultConn);
- request.setSQL(sql);
- return request;
- }
-
- public SessionMetaData getMetaData(ConnectionInfo conn) throws SQLException {
- int retries = 0;
-
- while(true) {
- String name = conn.getUser();
- String password = conn.getPassword();
- String db = conn.getDBString();
- Vector params = new Vector();
- params.addElement(new TextParam(0, db));
- params.addElement(new TextParam(0, name));
- params.addElement(new TextParam(0, password));
-
- try {
- Vector results = this._messgr.invokeMethod(1, params);
- SessionMetaData md = new SessionMetaData(this, results, conn);
- this._metadataObjs.addElement(md);
- return md;
- } catch (SQLConnectionException e) {
- ++retries;
- if (!this.logonFailed(conn, retries)) {
- throw e;
- }
- }
- }
- }
-
- public RelationView inquireDSN() throws SQLException {
- Vector results = this._messgr.invokeMethod(3);
- MultiView mv = new MultiView(this, results, new ConnectionInfo(""));
- this.addMultiView(mv);
- return mv.getRootRelView();
- }
-
- public void close() throws SQLException {
- if (!this._isClosed) {
- for(int i = 0; i < this._multiViews.size(); ++i) {
- MultiView mv = (MultiView)this._multiViews.elementAt(i);
-
- try {
- mv.close();
- } catch (SQLException var3) {
- }
- }
-
- this._multiViews.removeAllElements();
- this._metadataObjs.removeAllElements();
- this._messgr.invokeMethod(0);
- this._session.close();
- this._session = null;
- this._messgr = null;
- this._isClosed = true;
- }
-
- }
-
- private StringTokenizer parseURL(String url) throws SQLException {
- try {
- StringTokenizer tokens = new StringTokenizer(url);
- String protocol = tokens.nextToken(":");
- if (!protocol.equals("dbaw")) {
- throw new SQLException("Unrecognized protocol.");
- } else {
- String subname = tokens.nextToken("");
- return new StringTokenizer(subname);
- }
- } catch (NoSuchElementException var5) {
- throw new SQLException("Invalid URL syntax.");
- }
- }
-
- private Properties parseSubname(StringTokenizer tokens) throws SQLException {
- try {
- Properties properties = new Properties();
- String host = tokens.nextToken(":/");
- String port = tokens.nextToken(":/");
- ((Hashtable)properties).put("host", host);
- ((Hashtable)properties).put("port", port);
- return properties;
- } catch (NoSuchElementException var5) {
- throw new SQLException("Invalid subname syntax.");
- }
- }
-
- private void connect(String url) throws SQLException {
- this._session = null;
- this._messgr = null;
- this._isClosed = true;
- this._metadataObjs = new Vector();
- this._multiViews = new Vector();
-
- try {
- StringTokenizer subname = this.parseURL(url);
- Properties connectProperties = this.parseSubname(subname);
- String host = connectProperties.getProperty("host", "");
- int portNumber = Integer.parseInt(connectProperties.getProperty("port", "0"));
- this._session = new ClientSession(host, portNumber, "", true);
- this._messgr = new RemoteObject("CSCLClientSession", 0, this._session);
- } catch (Exception e) {
- throw new SQLException(((Throwable)e).toString());
- }
- }
-
- boolean logonFailed(ConnectionInfo conn, int retries) {
- return this._logonObj == null ? false : this._logonObj.logonFailed(conn, retries);
- }
-
- ClientSession getClientSession() {
- return this._session;
- }
-
- void addMultiView(MultiView mv) {
- this._multiViews.addElement(mv);
- }
- }
-